import { CHANGE_LOGIN_STATE, CHANGE_LOGIN_CODE } from './../mutation.types'
import { login } from '@/api'
const users = {
  namespaced: true,
  state: {
    login_code: '10119',
    // login_state: false
    login_state: localStorage.getItem('loginState') === 'ok' || false
  },
  actions: {
    login ({ commit }, params) {
      return new Promise(resolve => {
        login(params).then(res => {
          commit(CHANGE_LOGIN_CODE, res.data.code)
          if (res.data.code === '10888') {
            commit({
              type: CHANGE_LOGIN_STATE,
              payload: true
            })
          }
          resolve(res.data.data)
        })
      })
    }
  },
  mutations: {
    [CHANGE_LOGIN_STATE] (state, data) {
      state.login_state = data.payload
    },
    [CHANGE_LOGIN_CODE] (state, code) {
      state.login_code = code
    }
  }
}

export default users
